package com.lijian.reptile.model;

import com.lijian.reptile.ui.MyFrame;
import com.lijian.reptile.utils.WriteData;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * ClassName: Spider
 * Description:
 * date: 2021/11/24 0024 下午 2:21
 *
 * @author LiJian
 * @since JDK 1.8
 */
public class Spider implements Runnable{
    private  String url;


    public Spider(String url) {
        this.url = url;
    }

    @Override
    public void run() {

        try {
            Document document = Jsoup.connect(url).timeout(10000).get();
            Elements elements = document.select(".zjbox a");

            //获得标题
            String  titie = document.title();
            for (Element e:elements) {
                //System.out.println(e.text());
                //获取元素中的href
                String content = e.text();
                MyFrame.jTextArea.append(content + "\n");
                Document document1 = Jsoup.connect(url + e.attr("href")).get();
                content = document1.select("#content").text();
                //写入本地
              WriteData.writeData(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
